package com.ben.chatroom.common.zk;

import com.ben.chatroom.common.util.SpringContextUtil;
import lombok.Data;
import org.apache.curator.framework.CuratorFramework;

/**
 * @author
 */
@Data
public class CuratorZKclient {

  private String zkAddress;
  private String zkSessionTimeout;
  private CuratorFramework client;

  private static CuratorZKclient singleton = null;


  public static CuratorZKclient getSingleton() {
    if(singleton == null) {
      singleton = SpringContextUtil.getBean(CuratorZKclient.class);
    }

    return singleton;
  }

  public CuratorZKclient(String zkConnect, String zkSessionTimeout) {
    this.zkAddress = zkConnect;
    this.zkSessionTimeout = zkSessionTimeout;
    init();
  }

  private void init() {
    if(null != client) {
      return;
    }

    client = ClientFactory.createSimple(zkAddress, zkSessionTimeout);

    client.start();

  }

}
